<template>
	<el-container style="padding: 20px">
		<el-header>
			<el-button type="primary" @click="propertyListFunc" icon="el-icon-search" size="mini" :loading="loading">数据查询</el-button>
			<el-button type="primary" @click="exportFunc" icon="el-icon-folder-opened" size="mini" :disabled="loading || !tableData.length"
				>数据导出</el-button
			>
			<el-button type="primary" @click="fundFileShowFunc" icon="excel" size="mini" :disabled="loading"
				><svg-icon icon-class="excel" /> 文件导出</el-button
			>
		</el-header>
		<el-main>
			<el-collapse v-model="activeNames" @change="handleChange" accordion v-if="tableData.length">
				<el-collapse-item
					:title="`${item.baseInfo.fundCode}-${item.baseInfo.fundFullName}`"
					:name="`${index}`"
					v-for="(item, index) in tableData"
					:key="index"
				>
					<span v-if="item.baseInfo">
						<baseInfo :allProperty="allProperty" :itemData="item.baseInfo" />
					</span>
					<span v-if="item.dateInfo">
						<dateInfo :allProperty="allProperty" :itemData="item.dateInfo" />
					</span>
					<span v-if="item.organInfo">
						<originInfo :allProperty="allProperty" :itemData="item.organInfo" />
					</span>
					<span v-if="item.rateInfo">
						<rateInfo :allProperty="allProperty" :itemData="item.rateInfo" />
					</span>
					<span v-if="item.investInfo">
						<investInfo :allProperty="allProperty" :itemData="item.investInfo" />
					</span>
					<span v-if="item.riskInfo">
						<riskInfo :allProperty="allProperty" :itemData="item.riskInfo" />
					</span>
					<span v-if="item.fundManagerInfoList">
						<fundManagerInfoList :allProperty="allProperty" :itemData="item.fundManagerInfoList" :fundRaiseType="fundRaiseType" />
					</span>
					<span v-if="item.consignerInfo">
						<consignerInfo :allProperty="allProperty" :itemData="item.consignerInfo" />
					</span>
				</el-collapse-item>
			</el-collapse>
		</el-main>
		<div style="display: none">
			<div v-html="fundDatalist"></div>
			<div v-html="fundMange"></div>
			<div v-html="subFundType"></div>
			<div v-html="rateExcel"></div>
		</div>
		<propertyListCom
			:checckPersonList="checckPersonList"
			:managerList="managerList"
			:propertyListShow="propertyListShow"
			@propertyListFunc="propertyListFunc"
			@confrim="confrim"
			ref="propertyListCom"
		/>
		<FundFileExport
			:fundFileShow="fundFileShow"
			:checckPersonList="checckPersonList"
			:managerList="managerList"
			:enumData="enumData"
			@fundFileShowFunc="fundFileShowFunc"
		/>
	</el-container>
</template>
<script>
import { jsonArray } from '@/api/productApi/file'
import propertyListCom from './components/propertyList/index.vue' // 选择查询字段组件
import { exportFundData } from '@/api/graphQl/fundDataExport'
import { listItem } from './propertyData'
import baseInfo from './components/baseInfo/index.vue' // 基础信息
import dateInfo from './components/dateInfo/index.vue' // 日期信息
import rateInfo from './components/rateInfo/index.vue' // 费率信息
import originInfo from './components/originInfo/index.vue' // 机构信息
import investInfo from './components/investInfo/index.vue' // 投资相关信息
import riskInfo from './components/riskInfo/index.vue' // 风控信息
import fundManagerInfoList from './components/fundManagerInfoList/index.vue' // 基金经理信息
import consignerInfo from './components/consignerInfo/index.vue' // 委托人信息
import FundFileExport from './components/FundFileExport/index.vue' // 基金文件导出
import { dataConversionMethod, EnumList } from './DataConversion'
import { getEnum } from '@/api/productApi/file'
import { fundBaseData, excelTemplate, fundManageData, subordinateFunds, rateInfoExcel } from './exportMethod'
import { Loading } from 'element-ui'
import { getSpecialTypeInfo } from '@/api/productApi/infoMain'
import { getNewspaperList } from '@/api/productApi/newspaper'
import { departmentUser } from '@/api/productApi/fundManager'
import { fundAll, getAllCheck } from '@/api/productApi/infoMain'
export default {
	name: 'FundDataExport',
	components: {
		propertyListCom,
		baseInfo,
		dateInfo,
		originInfo,
		rateInfo,
		investInfo,
		fundManagerInfoList,
		consignerInfo,
		riskInfo,
		FundFileExport
	},
	data() {
		return {
			tableData: [],
			propertyListShow: false,
			allProperty: [],
			loading: false,
			activeNames: [],
			enumData: {},
			fundDatalist: '', // 基金基础数据模板
			fundMange: '', // 基金经理模板
			subFundType: '', // 下属基金模板
			rateExcel: '', // 费率模板
			dataType: '',
			specialTypeList: [],
			newSpaperList: [],
			userList: [],
			fundRaiseType: '',
			managerList: [],
			checckPersonList: [],
			fundFileShow: false
		}
	},

	mounted() {
		this.getEnumData()
		this.getUserOpList()
		this.getSpecialTypeInfo()
		this.getNewspaperListFunc()
	},

	methods: {
		// 查询：获取拥有某权限的用户信息
		getUserOpList() {
			this.checckPersonList = []
			this.managerList = []
			fundAll({ fundRaiseTypes: ['1'] }).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						item.fundRaiseType = '1'
					})
					this.managerList.push(...res.data.body.filter(item => !!item.personLiableId))
				}
			})
			fundAll({ fundRaiseTypes: ['2'] }).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						item.fundRaiseType = '2'
					})
					this.managerList.push(...res.data.body.filter(item => !!item.personLiableId))
				}
			})
			getAllCheck({ fundRaiseType: '1' }).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						item.fundRaiseType = '1'
					})
					this.checckPersonList.push(...res.data.body.filter(item => !!item.checkLiableId))
				}
			})
			getAllCheck({ fundRaiseType: '2' }).then(res => {
				if (res.msgCode === 'Y') {
					res.data.body.forEach(item => {
						item.fundRaiseType = '2'
					})
					this.checckPersonList.push(...res.data.body.filter(item => !!item.checkLiableId))
				}
			})
		},

		fundFileShowFunc() {
			this.fundFileShow = !this.fundFileShow
		},

		handleChange(val) {},

		exportFunc() {
			const loadingData = {
				text: '正在导出，请稍等',
				background: 'rgba(0, 0, 0, 0.8)',
				spinner: 'el-icon-loading'
			}
			Loading.service(loadingData)
			setTimeout(() => {
				if (this.dataType === '1') {
					// 判断是否是公募基金
					if ('branchShareList' in this.tableData[0].baseInfo) {
						// 判断是否有下属基金
						subordinateFunds(this, jsonArray(this.tableData))
					}
				}
				if ('fundManagerInfoList' in this.tableData[0]) {
					// 判断是否有基金经理
					fundManageData(this, jsonArray(this.tableData), this.fundRaiseType)
				}
				if ('rateInfo' in this.tableData[0]) {
					// 判断是否有费率列表
					if ('feeRateInfoList' in this.tableData[0].rateInfo) {
						rateInfoExcel(this, jsonArray(this.tableData), jsonArray(this.enumData))
					}
				}
				fundBaseData(this, jsonArray(this.tableData), jsonArray(this.allProperty))
				setTimeout(() => {
					excelTemplate(this.dataType)
					setTimeout(() => {
						// 下载后清空模板
						this.fundDatalist = '' // 基金基础数据模板
						this.fundMange = '' // 基金经理模板
						this.subFundType = '' // 下属基金模板
						this.rateExcel = '' // 费率模板
						const loadingInstance = Loading.service(loadingData)
						this.$nextTick(() => {
							loadingInstance.close()
						})
					})
				})
			})
		},

		propertyListFunc() {
			this.propertyListShow = !this.propertyListShow
		},

		// 获取所选数据
		confrim(obj) {
			this.fundRaiseType = obj.fundRaiseType
			this.activeNames = []
			const data = jsonArray(obj)
			this.allProperty = listItem(data.fundRaiseType)
			this.dataType = data.fundRaiseType
			this.loading = true
			this.$refs.propertyListCom.cancel()
			this.tableData = []
			const loadingData = {
				text: '正在查询，请稍等',
				background: 'rgba(0, 0, 0, 0.8)',
				spinner: 'el-icon-loading'
			}
			Loading.service(loadingData)
			exportFundData(data)
				.then(res => {
					if (res.data.getFundDetailInfoV1.fundDetailGraphqlDTOs) {
						res.data.getFundDetailInfoV1.fundDetailGraphqlDTOs.forEach(item => {
							if ('status' in item) {
								const enumObj = this.enumData.fundOperationStatus.find(items => items.typeCode === item.status)
								const status = enumObj ? enumObj.typeName : ''
								item.baseInfo = item.baseInfo ? { status, ...item.baseInfo } : {}
							}
							if ('personLiableId' in item) {
								const user = this.userList.find(items => items.username === item.personLiableId)
								const personLiableId = user ? user.firstName : ''
								item.baseInfo = item.baseInfo ? { personLiableId, ...item.baseInfo } : {}
							}
							if ('checkLiableId' in item) {
								const user = this.userList.find(items => items.username === item.checkLiableId)
								const checkLiableId = user ? user.firstName : ''
								item.baseInfo = item.baseInfo ? { checkLiableId, ...item.baseInfo } : {}
							}
						})
						this.tableData = dataConversionMethod(
							jsonArray(res.data.getFundDetailInfoV1.fundDetailGraphqlDTOs),
							jsonArray(this.enumData),
							jsonArray(this.specialTypeList),
							this.newSpaperList
						)
					}
				})
				.finally(() => {
					this.loading = false
					const loadingInstance = Loading.service(loadingData)
					this.$nextTick(() => {
						loadingInstance.close()
					})
				})
		},

		// 查询-特殊品种类型
		getSpecialTypeInfo() {
			getSpecialTypeInfo().then(res => {
				if (res.msgCode === 'Y') {
					this.specialTypeList = res.data.body
				}
			})
		},

		// 获取报社列表
		getNewspaperListFunc() {
			getNewspaperList().then(res => {
				if (res.msgCode === 'Y') {
					this.newSpaperList = res.data.body
				}
			})
		},

		// 获取枚举数据
		getEnumData() {
			// 查询：获取拥有某权限的用户信息
			departmentUser().then(res => {
				if (res.msgCode === 'Y') {
					this.userList = res.data.body
				}
			})
			getEnum([
				'productAssetReportType',
				'addCreditFormType',
				'addCreditOrganType',
				'investAdvisorOrgan',
				'feeRateAlgorithm',
				'peopleMax',
				'peopleMinNoBank',
				'peopleMinSpv',
				'csrcTypeOrgan',
				'csrcTypePersonal',
				'csrcTypeProduct',
				'idType',
				'idTypeOrgan',
				'idTypeProduct',
				'operateMode',
				'openFrequency',
				'fiduciaryDuty',
				'settleCurrency',
				'mixedInvestBias',
				'valuationWay',
				'valuationFrequency',
				'marketFundType',
				'marketTradeSite',
				'investTactics',
				'revenueFeature',
				'gradFundType',
				'riskGrade',
				'fundType',
				'subFundType',
				'productAssetType',
				'productSpecialType',
				'addcreditMeasure',
				'annuityPlan',
				'incomeDisWay',
				'planType',
				'privateFundType',
				'annuityPlanType',
				'annuityTrusteeMode',
				'addcreditMeasureType',
				'shareCategory', // 下属基金份额类别
				'custodyFeeAlgorithm',
				'salesServiceFeeAlgorithm',
				'associationOpenFrequency',
				'fundOperationStatus'
			]).then(res => {
				if (res.msgCode === 'Y') {
					EnumList.forEach(item => {
						if (item.enumKey !== 'csrcTypeOrgan' && res.data.body[item.enumKey]) {
							res.data.body[item.propertyKey] = res.data.body[item.enumKey]
						}
						if (item.enumKey === 'csrcTypeOrgan') {
							res.data.body[item.propertyKey] = [
								...res.data.body['csrcTypeOrgan'],
								...res.data.body['csrcTypePersonal'],
								...res.data.body['csrcTypeProduct']
							]
						}
					})
					this.enumData = res.data.body
				}
			})
		}
	}
}
</script>
<style lang="scss" src='./index.scss'>
</style>
